import {onMounted, reactive} from 'vue';
import {deleteDb, queyrOdd} from '@/api/url/erp/baseMessage';
import { sliceTime } from '@/utils/time';
import router from '@/router';
import {sfcteGetExcel} from "@api/url/base";
import notification from "@arco-design/web-vue/es/notification";
export default function importConfig() {
  const base = reactive<any>({
    emptyCheckForm:{},
    checkForm: {
      TA001: '',
      TA002: '',
      TA006: '',
    },
    data: [],
    delVisible: false,
    pagination: {
      pageNum: 1,
      pageSize: 10,
      total: 0,
    },
    loadingFlag: false,
    currentInfo:{},
  });

  // 数据读取
  const getTable = () => {
    base.loadingFlag = true;
    let obj={
      pageNum: base.pagination.pageNum,
      pageSize:base.pagination.pageSize,
    };

    let rel=Object.assign(obj,base.checkForm);

    queyrOdd(rel).then((res) => {
      if (res.code == 200 && res.data) {
        base.pagination.total = res.data.total;
        base.data = res.data.data.map((item) => {
          item.kgtime = sliceTime(item.kgtime).replaceAll(":","");
          if(item.percent > 100){
            item.percent = 100;
          }
          if(item.percent == '.0'){
            item.percent = 0;
          }
          return item;
        });
        base.loadingFlag = false;
      }
    });
  };

  //查询
  const search=()=>{
    base.pagination.pageNum=1;
    getTable();
  };

  // 重置
  const reset = () => {
    base.checkForm=JSON.parse(JSON.stringify(base.emptyCheckForm));
    getTable();
  };

  //存储查询条件
  const storeCondition=()=>{
    localStorage.setItem('queyrOddModelFormData',JSON.stringify(base.checkForm));
  };

  // 跳到详情页
  const toDetail = (row) => {
    storeCondition();

    router.push({
      path: '/querySfCteModel',
      query: { gx: row.ta003.trim(), odd: row.ta001.trim(), single: row.ta002.trim(),source:1 },
    });
  };

  // 下载
  const outExcleFun = (row) => {
    let obj={
      single:row.ta002,
      odd:row.ta001,
      gx:row.ta003
    };
    sfcteGetExcel(obj).then(res=>{
      if(res.code==201){
        notification.info(res.data);
      }else{
        downloadFile(import.meta.env.VITE_APP_API_BASE_URL + `/sfcte/getExcel?single=${row.ta002}&odd=${row.ta001}&gx=${row.ta003}`);
      }
    });


    // downloadFile(import.meta.env.VITE_APP_API_BASE_URL + `/sfcte/getExcel?single=${row.single}&odd=${row.odd}&gx=${row.gx}`);
    // console.log( import.meta.env.VITE_APP_API_BASE_URL);
  };

  // 下载
  const downloadFile=(url)=>{
    const link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.download = url.substring(url.lastIndexOf('/') + 1, url.length);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  };

  onMounted(()=>{
    base.emptyCheckForm=JSON.parse(JSON.stringify(base.checkForm));
    let relt=JSON.parse(localStorage.getItem('queyrOddModelFormData'));
    if(relt){
      base.checkForm=relt;
    }
    getTable();
  });

  return {
    base,
    getTable,
    reset,
    outExcleFun,
    toDetail,
    search,
  };
}
